package cn.edu.zknu.mita.service.impl;

import cn.edu.zknu.mita.mapper.CategoryMapper;
import cn.edu.zknu.mita.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created by iwlnner on 2020/3/27.
 */
@Service
public class CategoryService implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public String convertCategory(String categoryIds) {
        if (categoryIds == null || "".equals(categoryIds.trim())) {
            return "";
        } else {
            if (!categoryIds.contains(",")) {
                return categoryMapper.queryCategoryName(categoryIds);
            }else {
                List<String> names = categoryMapper.queryCategoryNames(Arrays.asList(categoryIds.split(",")));
                StringBuilder sb = new StringBuilder(20);
                for (String name : names) {
                    sb.append(name).append("  ");
                }
                String ret = sb.toString();
                return ret.substring(0, ret.lastIndexOf("  "));
            }
        }
    }
}
